#!/usr/bin/env python
# -*- coding: utf-8 -*-
#author : sixther.me
#date : 2014-10-11
import os
import sys
import MySQLdb
import threading

LITB_DBHost='10.1.1.1'
MINI_DBHost='10.1.1.2'
LITB_DBName='xxxxxx'
MINI_DBName='xxxxxx'
DBUser='xxxxxx'
DBPassword='xxxxxxx'


litb_conn=MySQLdb.connect(host=LITB_DBHost,user=DBUser,passwd=DBPassword,db=LITB_DBName,charset='utf8')
mini_conn=MySQLdb.connect(host=MINI_DBHost,user=DBUser,passwd=DBPassword,db=MINI_DBName,charset='utf8')
db_conn_list={'litb':litb_conn,'mini':mini_conn}
site_list={'mobile':'%VelaOrderChannel:mobile_web%','app':'%VelaOrderChannel:mobile_app_%'}
def fetch_list_sql(db_conn,sql):
        db_cursor=db_conn.cursor()
        db_cursor.execute(sql)
        result=db_cursor.fetchall()
#        result='%s' % zabbix_cursor.fetchone()
#       zabbix_conn.commit()
       	db_cursor.close()
        return result

def fetch_one_sql(db_conn,sql): 
       	db_cursor=db_conn.cursor()
        db_cursor.execute(sql)
        result='%s' % db_cursor.fetchone()
        db_cursor.close()
        return result

def check_total_order(db_conn,timestamp):
	sql='select count(*) from test_orders where date_purchased>DATE_SUB(adddate(now(),-' + timestamp +'),INTERVAL 30 MINUTE) and  date_purchased<adddate(now(),-' + timestamp +')'
	return int(fetch_one_sql(db_conn,sql))
def check_site_total_order(db_conn,site,timestamp):
	if site == 'pc':
		sql="select count(*) from test_orders where date_purchased>DATE_SUB(adddate(now(),-" + timestamp + "),INTERVAL 30 MINUTE) and  date_purchased<adddate(now(),-" +  timestamp + ") and orders_referral not like '%VelaOrderChannel%'"
	else:
		sql="select count(*) from test_orders where date_purchased>DATE_SUB(adddate(now(),-" + timestamp + "),INTERVAL 30 MINUTE) and  date_purchased<adddate(now(),-" +  timestamp + ") and orders_referral like '" + site_list[site] + "'"
	return int(fetch_one_sql(db_conn,sql))
def check_language_order(db_conn,language):
	sql="select count(*) from test_languages l,test_orders o where l.languages_id=o.orders_language and date_purchased>DATE_SUB(now(),INTERVAL 30 MINUTE) and l.name='" + language + "'"
	return int(fetch_one_sql(db_conn,sql))
def check_payment_order(db_conn,payment):
	sql="select count(*) from test_orders where date_purchased>DATE_SUB(now(),INTERVAL 30 MINUTE) and payment_module_code='" + payment + "'"
	return int(fetch_one_sql(db_conn,sql))
def check_order_value(db_conn,timestamp):
	sql='select sum(order_total/currency_value) from test_orders where date_purchased>DATE_SUB(adddate(now(),-' + timestamp +'),INTERVAL 30 MINUTE) and  date_purchased<adddate(now(),-' + timestamp + ')'
	return(fetch_one_sql(db_conn,sql))
def check_order_import_num(db_conn):
	sql='select count(*) from test_order_import_history where create_time>DATE_SUB(now(),INTERVAL 1 HOUR)'
	return(fetch_one_sql(db_conn,sql))
def check_order_difference(db_conn,site):
	if site ==  'total':
		average_last_3_days=(check_total_order(db_conn,'1') + check_total_order(db_conn,'7') + check_total_order(db_conn,'14')) / 3
		today_num=check_total_order(db_conn,'0')
	else:
		average_last_3_days=(check_site_total_order(db_conn,site,'1') + check_site_total_order(db_conn,site,'7') + check_site_total_order(db_conn,site,'14')) / 3
		today_num=check_site_total_order(db_conn,site,'0')
	if today_num < average_last_3_days:
		difference=(float(average_last_3_days) - float(today_num)) / float(average_last_3_days) * 100
		return difference
	else:
		return 0
if __name__ ==  '__main__':
	if sys.argv[2] == 'total':
		print check_total_order(db_conn_list[sys.argv[1]],sys.argv[3])
	elif sys.argv[2] == 'language':
		print check_language_order(db_conn_list[sys.argv[1]],sys.argv[3])
	elif sys.argv[2] == 'payment':
		print check_payment_order(db_conn_list[sys.argv[1]],sys.argv[3])
	elif sys.argv[2] == 'value':
		print "%.2f" % float(check_order_value(db_conn_list[sys.argv[1]],sys.argv[3]))
	elif sys.argv[2] == 'site':
		print check_site_total_order(db_conn_list[sys.argv[1]],sys.argv[3],sys.argv[4])
	elif sys.argv[2] == 'import':
		print check_order_import_num(db_conn_list[sys.argv[1]])
	elif sys.argv[2] == 'difference':
		print  "%.2f" % float(check_order_difference(db_conn_list[sys.argv[1]],sys.argv[3]))
	else:
		print 'Usage error'
